/**
 * 
 */
package com.social.itba.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.MapUtils;

import com.social.itba.model.Movie;

/**
 * Service that performs opertions over the movies. 
 * 
 * @author waabox( emiliano[dot]arango[at]globat[dot]com )
 *
 */
public class MovieService {
  /**
   * Single instance. 
   */
  private static final MovieService instance = new MovieService();
  /**
   * Map of movies.
   */
  private Map<Integer, Movie> movies;
  /**
   * default constructor.
   */
  @SuppressWarnings("unchecked")
  private MovieService() {
    movies = MapUtils.synchronizedMap(new HashMap<Integer, Movie>());
  }
  /**
   * @param id {@link Integer} movie to search.
   * @return {@link Movie}
   */
  public static Movie get(final Integer id) {
    return instance.movies.get(id);
  }
  /**
   * @param id {@link Integer} of the movie to delete.
   */
  public static void remove(final Integer id) {
    instance.movies.remove(id);
  }
  /**
   * @param movie {@link Movie} movie to add.
   */
  public static void add(final Movie movie) {
    instance.movies.put(movie.getId(), movie);
  }
  /**
   * Retrieve all movies.
   * Bad Practice!
   * return {@link List} of {@link Movie}.
   */
  public static List<Movie> getAll() {
    List<Movie> out = new ArrayList<Movie>();
    Set<Integer> set = instance.movies.keySet();
    for (Integer id : set) {
      out.add(instance.movies.get(id));
    }
    return out;
  }
}
